﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Dim tb_email As Object

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM profile WHERE author=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("firstName")) Then tb_firstname.Text = dr("firstName")
                If Not IsDBNull(dr("lastName")) Then tb_lastname.Text = dr("lastName")
                If Not IsDBNull(dr("email")) Then tb_mail.Text = dr("email")
                If Not IsDBNull(dr("country")) Then tb_country.Text = dr("country")
                If Not IsDBNull(dr("school")) Then tb_school.Text = dr("school")
                If Not IsDBNull(dr("websiteURL")) Then tb_website.Text = dr("websiteURL")

            End If
        End If
    End Sub

    Protected Sub btn_saveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_saveProfile.Click
        Dim newFileName As String
        Dim SqlString As String
        If fu_picture.HasFile Then

            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"

            If fu_picture.HasFile Then
                fu_picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
            End If
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)

        If fu_picture.HasFile Then
            SqlString = "UPDATE profile SET firstName=@f1, lastName=@f2, country=@f3, school=@f4, websiteURL=@f5, userPhoto=@f6, email=@f7 WHERE author=@f8"
        Else
            SqlString = "UPDATE profile SET firstName=@f1, lastName=@f2, country=@f3, school=@f4, websiteURL=@f5, email=@f7 WHERE author=@f8"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_firstname.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastname.Text)
        cmd.Parameters.AddWithValue("@f3", tb_country.Text)
        cmd.Parameters.AddWithValue("@f4", tb_school.Text)
        cmd.Parameters.AddWithValue("@f5", tb_website.Text)

        If fu_picture.HasFile Then
            cmd.Parameters.AddWithValue("@f6", "pictures/" & newFileName)
        End If

        cmd.Parameters.AddWithValue("@f7", tb_mail.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("profile.aspx?author=" & User.Identity.Name)


    End Sub


End Class